const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin'); //通过 npm 安装
const webpack = require('webpack'); //访问内置的插件
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const Mock = require('mockjs');
const data = Mock.mock({
    'list|10': [{
        img: '@Image(100x100,@color)',
        title: '@ctitle'
    }]
})




module.exports = {
    entry: './src/index.js',
    output: {
        filename: 'main.js',
        path: path.join(__dirname, './dist')
    },
    plugins: [
        new HtmlWebpackPlugin({ template: './src/index.html' }),
        new MiniCssExtractPlugin()
    ],
    mode: 'development',
    module: {
        rules: [{
                test: /\.s[ac]ss$/,
                use: [
                    // Creates `style` nodes from JS strings
                    // "style-loader",
                    MiniCssExtractPlugin.loader,
                    // Translates CSS into CommonJS
                    "css-loader",
                    // Compiles Sass to CSS
                    "sass-loader",
                ],
            },
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env']
                    }
                }
            }
        ]
    },
    devServer: {
        host: 'localhost',
        port: 9090,
        hot: true,
        open: true,
        before(app) {
            app.get('/list', (req, res) => {
                res.send(data.list)
            })
        }
    }
}